#include "InputManager.h"
#include "Platforms.h"

InputManager* InputManager::s_instance = NULL;

InputManager::InputManager(void)
{
	for (int i = 0; i < MAX_256; i++)
	{
		m_keyCode[i] = false;
	}

	m_typeMouse = -1;
	m_xMouse = -1;
	m_yMouse = -1;
}

InputManager::~InputManager(void)
{
}

InputManager* InputManager::GetInstance()
{
	if (s_instance == NULL)
	{
		s_instance = new InputManager();
	}

	return s_instance;
}

void InputManager::Destroy()
{
	if (s_instance != NULL)
	{
		delete s_instance;
		s_instance = NULL;
	}
}

void InputManager::OnKey(int keyCode, bool isKeyDown)
{
	m_keyCode[keyCode] = isKeyDown;
	LOGI("\nKey code: %d - is key down: %d", keyCode, isKeyDown);
}

void InputManager::OnMouse(int type, int x, int y)
{
	// type 0 - Cick
	// type 1 - Release
	// type 2 - Drag

	m_typeMouse = type;
	m_xMouse = x;
	m_yMouse = y;
	LOGI("\nMouse type: %d - (x = %d, y = %d)", type, x, y);
}

bool InputManager::IsKeyDown(int keyCode)
{
	if (m_keyCode[keyCode] == 1)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

bool InputManager::IsMouseDown()
{
	if (m_typeMouse == 0)
	{
		return 1;
	}
	else return 0;
}

bool InputManager::IsMouseUp()
{
	if (m_typeMouse == 1)
	{
		return 1;
	}
	else return 0;
}

bool InputManager::IsDrag()
{
	if (m_typeMouse == 2)
	{
		return 1;
	}
	else return 0;
}

bool InputManager::IsPointerInRect(int x, int y, int w, int h)
{
	bool isPointerInRect = false;
	if (IsMouseDown())
	{
		if ((m_xMouse >= x && m_xMouse <= x + w) && (m_yMouse >= y && m_yMouse <= y + h))
		{
			isPointerInRect = true;
		}
	}
	return isPointerInRect;
}

int InputManager::GetXmouse()
{
	return m_xMouse;
}

int InputManager::GetYmouse()
{
	return m_yMouse;
}